package datastructure.tree;

/**
 * 顺序存储二叉树
 *
 * @author jack.wu
 * @version 1.0
 * @date 2020-03-27
 */
public class ArrBinaryTreeDemo {

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7};
        ArrBinaryTree binaryTree = new ArrBinaryTree(arr);
        binaryTree.preOrder();
    }
}

class ArrBinaryTree {

    private int[] arr;

    public ArrBinaryTree(int[] arr) {
        this.arr = arr;
    }

    public void preOrder() {
        preOrder(0);
    }

    /**
     * 前序打印数组
     *
     * @param index
     */
    public void preOrder(int index) {
        if (arr == null || arr.length == 0) {
            System.out.println("数组为空~~~~~");
        }
        System.out.println(arr[index]);
        if ((index * 2 + 1) < arr.length) {
            // 左递归
            preOrder(index * 2 + 1);
        }
        if ((index * 2 + 2) < arr.length) {
            // 右递归
            preOrder(index * 2 + 2);
        }
    }
}
